package com.lagou.no3

case class UserInfo(userName: String, location: String, startTime: Int, duration: Int)
object LocationTime {
    def main(args: Array[String]): Unit = {
        val userInfoList:List[UserInfo] = List(
            UserInfo("UserA","LocationA",8,60),
            UserInfo("UserA","LocationA",9,60),
            UserInfo("UserB", "LocationB", 10, 60),
            UserInfo("UserB", "LocationB", 11, 80) ,
            UserInfo("UserA", "LocationB", 11, 60) ,
            UserInfo("UserA", "LocationB", 12, 70)
        )
        //根据userName和location作为主键对数据分组
        val groupUserMap = userInfoList.groupBy(t => t.userName + "," + t.location)
        //对分组好的数据的value根据starttime进行排序
        val orderUserMap = groupUserMap.mapValues(t => t.sortBy(x => x.startTime))
        var first_time = 0;
        val sum = orderUserMap.mapValues(t => {
            //获取每个map中的list对象的第一个对象的时间，因为上面排过序了 所以这个肯定是最早的那个时间
            first_time = t.head.startTime
            //对每个时长进行累加
            t.map(x => x.duration).sum
        })
        //打印
        sum.foreach{
            case(datas,sumTime) => println(s"$datas,$first_time,$sumTime")
        }

    }
}
